package com.ibox;

import android.app.Activity;
import android.app.Application;

import com.ibox.utils.AppUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * Project IBox
 * Created by Amos
 * Created on 2017-09-25
 * Desc
 */

public class BaseApplication extends Application {
    private static BaseApplication mInstance;
    private Set<Activity> allActivities;

    public static synchronized BaseApplication getInstance() {
        return mInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        AppUtils.init(this);
    }

    /**
     * 添加activity
     *
     * @param act
     */
    public void addActivity(Activity act) {
        if (allActivities == null) {
            allActivities = new HashSet<>();
        }
        allActivities.add(act);
    }

    /**
     * 移除activity
     *
     * @param act
     */
    public void removeActivity(Activity act) {
        if (allActivities != null) {
            allActivities.remove(act);
        }
    }

    /**
     * 退出程序
     */
    public void exitApp() {
        if (allActivities != null) {
            synchronized (allActivities) {
                for (Activity act : allActivities) {
                    act.finish();
                }
            }
        }
//        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
